package com.online.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.entity.ApiResult;
import com.online.entity.Score;
import com.online.service.ScoreService;
import com.online.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分数
 */
@RestController
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @GetMapping("/scores")
    public ApiResult findAll(){
        List<Score> res=scoreService.findAll();
        return ApiResultHandler.buildApiResult(200,"查询所有学生成绩","res");
    }
    @GetMapping("/score/{page}/{size}/{studentId}")
    public ApiResult findById(@PathVariable("page") Integer page,@PathVariable("size") Integer size,
                              @PathVariable("studentId") Integer studentId){
        Page<Score> scorePage=new Page<>(page,size);
        IPage<Score> res=scoreService.findById(scorePage,studentId);
        return ApiResultHandler.buildApiResult(200,"根据ID查询成绩",res);
    }
    @PostMapping("/score")
    public ApiResult add(@RequestBody Score score){
        int res=scoreService.add(score);
        if (res==0){
            return ApiResultHandler.buildApiResult(400,"添加成绩失败",res);
        }else{
            return ApiResultHandler.buildApiResult(200,"添加成绩成功",res);
        }
    }
    @GetMapping("/score/{examCode}")
    public ApiResult findByExamCode(@PathVariable("examCode") Integer examCode){
        List<Score> scores=scoreService.findByExamCode(examCode);
        return ApiResultHandler.buildApiResult(200,"查询成功",scores);
    }
}
